package com.app114.app.android.rk.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.speech.RecognizerIntent;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.widget.Toast;

import com.app114.app.android.rk.conf.Conf;

/**
 * 设备参数获取工具
 * 
 * 
 */
public class DeviceUtil {
	/**
	 * get http request UserAgent
	 * 
	 * @param context
	 * @return
	 */
	public static void setUserAgent(Context context) {
		if (Conf.Usr_Agent == null) {
			Conf.Usr_Agent = new WebView(context).getSettings().getUserAgentString();
		}
	}

	/**
	 * get imsi
	 * 
	 * @param context
	 * @return
	 */
	public static String getIMSI(Context context) {
		TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = mTelephonyMgr.getSubscriberId();

		return imsi;
	}

	public static String getIMEI(Context context) {
		TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = mTelephonyMgr.getDeviceId();

		if (imei == null || imei.length() <= 0) {

			try {
				Class<?> c = Class.forName("android.os.SystemProperties");
				Method get = c.getMethod("get", String.class);

				imei = (String) get.invoke(c, "ro.serialno");
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return imei;
	}

	public static String getPN() {
		return Build.MODEL;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static String getScreenSize(Context context) {
		Display screenSize = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int width = screenSize.getWidth();
		int height = screenSize.getHeight();
		return width + "*" + height;
	}

	public static boolean isLandscape(Context context) {
		Display screenSize = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int width = screenSize.getWidth();
		int height = screenSize.getHeight();
		return width > height;
	}
	
	public static Display getDisplay(Context context) {
		return ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
	}

	/**
	 * transform int to ip string
	 * 
	 * @param i
	 * @return
	 */
	public static String intToIp(int i) {
		return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
	}

	public static void voice(Activity mActivity) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.ftrd.socialtv.SearchActivity");
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "豆腕儿开始聆听你喊话！");
		mActivity.startActivityForResult(intent, Conf.VOICE_RECOGNITION_REQUEST_CODE);
	}

	/**
	 * 
	 * @param context
	 * @param intent
	 */
	public static void startctivity(Context context, Intent intent) {
		context.startActivity(intent);
	}

	/**
	 * 
	 * @param context
	 * @param message
	 */
	public static void toast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 
	 * @param context
	 * @param animId
	 * @param v
	 */
	public static void animation(Context context, int animId, View v) {
		v.startAnimation(AnimationUtils.loadAnimation(context, animId));
	}

	/**
	 * 
	 * @param v
	 */
	public static void toggleAnimation(View v) {
		TranslateAnimation anim = new TranslateAnimation(-100, 0, 0, 0);
		anim.setInterpolator(new BounceInterpolator());
		anim.setDuration(1000);
		v.startAnimation(anim);
	}

}
